/*
Copyright (c) 2012 the VATSIM Connector project

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "planestax.h"
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <utility>

void designatorVisitor(std::string const& name, std::list<std::string> const& taxa, unsigned minmatch)
{
  std::cout << name << "\t" << minmatch;
  for (std::list<std::string>::const_iterator it = taxa.begin(); it != taxa.end(); ++it)
    std::cout << "\t" << *it;
  std::cout << std::endl;
}

void planeVisitor(std::string const& path, std::list<std::string> const& taxa)
{
  for (std::list<std::string>::const_iterator it = taxa.begin(); it != taxa.end(); ++it)
    std::cout << *it << "\t";
  std::cout << path << std::endl;
}

int main()
{
  std::string designators (
    "A318	4	A320	2	Jet	M	Landplane\n"
    "A319	4	A320	2	Jet	M	Landplane\n"
    "A320	4	A320	2	Jet	M	Landplane\n"
    "A321	4	A320	2	Jet	M	Landplane\n"
    "A332	4	A330	2	Jet	H	Landplane\n"
    "A333	4	A330	2	Jet	H	Landplane\n"
    "A342	4	A340	4	Jet	H	Landplane\n"
    "A343	4	A340	4	Jet	H	Landplane\n"
    "A345	4	A340	4	Jet	H	Landplane\n"
    "A346	4	A340	4	Jet	H	Landplane\n"
    "B731	4	B737	2	Jet	M	Landplane\n"
    "B732	4	B737	2	Jet	M	Landplane\n"
    "B733	4	B737	2	Jet	M	Landplane\n"
    "B734	4	B737	2	Jet	M	Landplane\n"
    "B735	4	B737	2	Jet	M	Landplane\n"
    "B736	4	B737	2	Jet	M	Landplane\n"
    "B737	4	B737	2	Jet	M	Landplane\n"
    "B738	4	B737	2	Jet	M	Landplane\n"
    "B739	4	B737	2	Jet	M	Landplane\n"
    "B741	4	B747	4	Jet	H	Landplane\n"
    "B742	4	B747	4	Jet	H	Landplane\n"
    "B743	4	B747	4	Jet	H	Landplane\n"
    "B744	4	B747	4	Jet	H	Landplane\n"
    "B748	4	B747	4	Jet	H	Landplane\n"
    "B74D	4	B747	4	Jet	H	Landplane\n"
    "B74R	4	B747	4	Jet	H	Landplane\n"
    "B74S	4	B747	4	Jet	H	Landplane\n"
    "B752	4	B757	2	Jet	M	Landplane\n"
    "B753	4	B757	2	Jet	M	Landplane\n"
    "B762	4	B767	2	Jet	H	Landplane\n"
    "B763	4	B767	2	Jet	H	Landplane\n"
    "B764	4	B767	2	Jet	H	Landplane\n"
    "C170	4	C170	1	Piston	L	Landplane\n"
    "C172	4	C170	1	Piston	L	Landplane\n"
    "C175	4	C170	1	Piston	L	Landplane\n"
    "C177	4	C170	1	Piston	L	Landplane\n"
    "C180	4	C180	1	Piston	L	Landplane\n"
    "C182	4	C180	1	Piston	L	Landplane\n"
    "C185	4	C180	1	Piston	L	Landplane\n"
    "C188	4	C180	1	Piston	L	Landplane\n"
    "ZZZZ	1	ZZZZ	1	Piston	L	Landplane\n"
    );
  std::string planes (
    "ZZZZ	YYY	SYS	aircraft/jets/heavy/a320/united.obj\n"
    "A319	UAL|COA	SYS	aircraft/jets/heavy/a319/united.obj\n"
    "A320	ADR	SYS	aircraft/jets/heavy/a320/adria.obj\n"
    "A320	EIN|REA	SYS	aircraft/jets/heavy/a320/aer_lingus.obj\n"
    "A320	AFL	SYS	aircraft/jets/heavy/a320/aeroflot.obj\n"
    "A320	ARG	SYS	aircraft/jets/heavy/a320/aerolineas_argentinas.obj\n"
    "A320	BER	SYS	aircraft/jets/heavy/a320/air_berlin.obj\n"
    "A320	ACA|JZA|SKV|GGN	SYS	aircraft/jets/heavy/a320/air_canada.obj\n"
    "A320	AFR|BCY|RAE|BZH	SYS	aircraft/jets/heavy/a320/air_france.obj\n"
    "A320	AMC	SYS	aircraft/jets/heavy/a320/air_malta.obj\n"
    "A320	AZA	SYS	aircraft/jets/heavy/a320/alitalia.obj\n"
    "A320	AUA	SYS	aircraft/jets/heavy/a320/austrian.obj\n"
    "A320	BAW|SHT|CFE|SUS|CAW|BOS|LOG	SYS	aircraft/jets/heavy/a320/british_airways.obj\n"
    "A320	CDN	SYS	aircraft/jets/heavy/a320/canadian.obj\n"
    "A320	CPA	SYS	aircraft/jets/heavy/a320/cathay_pacific.obj\n"
    "A320	CES	SYS	aircraft/jets/heavy/a320/china_eastern.obj\n"
    "A320	CTN	SYS	aircraft/jets/heavy/a320/croatia_airlines.obj\n"
    "A320	DHV|DHX|DHK|DAE|BCS|BDA	SYS	aircraft/jets/heavy/a320/dhl.obj\n"
    "A320	EZY|EZS	SYS	aircraft/jets/heavy/a320/easyjet.obj\n"
    "A320	ELY	SYS	aircraft/jets/heavy/a320/el_al_israel.obj\n"
    "A320	UAE	SYS	aircraft/jets/heavy/a320/emirates.obj\n"
    "A320	FDX	SYS	aircraft/jets/heavy/a320/fedex.obj\n"
    "A320	FFT|CHQ|SSX	SYS	aircraft/jets/heavy/a320/frontier.obj\n"
    "A320	GWI	SYS	aircraft/jets/heavy/a320/germanwings.obj\n"
    "A320	IBE|ANE|IBS	SYS	aircraft/jets/heavy/a320/iberia.obj\n"
    "A320	JAZ	SYS	aircraft/jets/heavy/a320/jalways.obj\n"
    "A320	JAL|JEX|JLJ|JTA	SYS	aircraft/jets/heavy/a320/japan_airlines.obj\n"
    "A320	RJA	SYS	aircraft/jets/heavy/a320/jordanian.obj\n"
    "A320	KLM	SYS	aircraft/jets/heavy/a320/klm.obj\n"
    "A320	LDA	SYS	aircraft/jets/heavy/a320/lauda_air.obj\n"
    "A320	LTU	SYS	aircraft/jets/heavy/a320/ltu.obj\n"
    "A320	DLH|CLH|GEC|LHT	SYS	aircraft/jets/heavy/a320/lufthansa.obj\n"
    "A320	ISS	SYS	aircraft/jets/heavy/a320/meridiana.obj\n"
    "A320	NWA|CPZ	SYS	aircraft/jets/heavy/a320/northwest.obj\n"
    "A320	PTI	SYS	aircraft/jets/heavy/a320/privatair.obj\n"
    "A320	QFA|QLK|QNZ	SYS	aircraft/jets/heavy/a320/qantas.obj\n"
    "A320	SAS|CNO	SYS	aircraft/jets/heavy/a320/sas.obj\n"
    "A320	SIA|SQC	SYS	aircraft/jets/heavy/a320/singapore.obj\n"
    "A320	JKK	SYS	aircraft/jets/heavy/a320/spanair.obj\n"
    "A320	SXS	SYS	aircraft/jets/heavy/a320/sunexpress.obj\n"
    "A320	SWR|SWU	SYS	aircraft/jets/heavy/a320/swiss.obj\n"
    "A320	TCX|TCW|VKG	SYS	aircraft/jets/heavy/a320/thomas_cook.obj\n"
    "A320	TAY	SYS	aircraft/jets/heavy/a320/tnt.obj\n"
    "A320	TUI|JAF|TOM|TFL|CRL|BLX|JFU|HLF	SYS	aircraft/jets/heavy/a320/tuifly.obj\n"
    "A320	UAL|COA	SYS	aircraft/jets/heavy/a320/united.obj\n"
    "A320	UPS	SYS	aircraft/jets/heavy/a320/ups.obj\n"
    "A332	AWE|USA	SYS	aircraft/jets/heavy/a330/us_airways.obj\n"
    "A342	EIN|REA	SYS	aircraft/jets/heavy/a340/aer_lingus.obj\n"
    "A342	AFL	SYS	aircraft/jets/heavy/a340/aeroflot.obj\n"
    "A342	ARG	SYS	aircraft/jets/heavy/a340/aerolineas_argentinas.obj\n"
    "A342	BER	SYS	aircraft/jets/heavy/a340/air_berlin.obj\n"
    "A342	ACA|JZA|SKV|GGN	SYS	aircraft/jets/heavy/a340/air_canada.obj\n"
    "A342	AFR|BCY|RAE|BZH	SYS	aircraft/jets/heavy/a340/air_france.obj\n"
    "A342	AMC	SYS	aircraft/jets/heavy/a340/air_malta.obj\n"
    "A342	AZA	SYS	aircraft/jets/heavy/a340/alitalia.obj\n"
    "A342	BAW|SHT|CFE|SUS|CAW|BOS|LOG	SYS	aircraft/jets/heavy/a340/british_airways.obj\n"
    "A342	CPA	SYS	aircraft/jets/heavy/a340/cathay_pacific.obj\n"
    "A342	CES	SYS	aircraft/jets/heavy/a340/china_eastern.obj\n"
    "A342	CTN	SYS	aircraft/jets/heavy/a340/croatia_airlines.obj\n"
    "A342	DHV|DHX|DHK|DAE|BCS|BDA	SYS	aircraft/jets/heavy/a340/dhl.obj\n"
    "A342	EZY|EZS	SYS	aircraft/jets/heavy/a340/easyjet.obj\n"
    "A342	ELY	SYS	aircraft/jets/heavy/a340/el_al_israel.obj\n"
    "A342	UAE	SYS	aircraft/jets/heavy/a340/emirates.obj\n"
    "A342	FDX	SYS	aircraft/jets/heavy/a340/fedex.obj\n"
    "A342	GWI	SYS	aircraft/jets/heavy/a340/germanwings.obj\n"
    "A342	IBE|ANE|IBS	SYS	aircraft/jets/heavy/a340/iberia.obj\n"
    "A342	JAZ	SYS	aircraft/jets/heavy/a340/jalways.obj\n"
    "A342	JAL|JEX|JLJ|JTA	SYS	aircraft/jets/heavy/a340/japan_airlines.obj\n"
    "A342	KLM	SYS	aircraft/jets/heavy/a340/klm.obj\n"
    "A342	LDA	SYS	aircraft/jets/heavy/a340/lauda_air.obj\n"
    "A342	LTU	SYS	aircraft/jets/heavy/a340/ltu.obj\n"
    "A342	DLH|CLH|GEC|LHT	SYS	aircraft/jets/heavy/a340/lufthansa.obj\n"
    "A342	ISS	SYS	aircraft/jets/heavy/a340/meridiana.obj\n"
    "A342	PTI	SYS	aircraft/jets/heavy/a340/privatair.obj\n"
    "A342	QFA|QLK|QNZ	SYS	aircraft/jets/heavy/a340/qantas.obj\n"
    "A342	SAS|CNO	SYS	aircraft/jets/heavy/a340/sas.obj\n"
    "A342	SIA|SQC	SYS	aircraft/jets/heavy/a340/singapore.obj\n"
    "A342	SXS	SYS	aircraft/jets/heavy/a340/sunexpress.obj\n"
    "A342	SWR|SWU	SYS	aircraft/jets/heavy/a340/swiss.obj\n"
    "A342	TAY	SYS	aircraft/jets/heavy/a340/tnt.obj\n"
    "A342	TUI|JAF|TOM|TFL|CRL|BLX|JFU|HLF	SYS	aircraft/jets/heavy/a340/tuifly.obj\n"
    "A342	UAL|COA	SYS	aircraft/jets/heavy/a340/united.obj\n"
    "A342	UPS	SYS	aircraft/jets/heavy/a340/ups.obj\n"
    "C172	YYY	SYS	aircraft/props/ga/c172.obj\n"
    "C180	YYY	SYS	aircraft/props/ga/c180j.obj\n"
    "C182	YYY	SYS	aircraft/props/ga/c182m.obj\n"
    "C185	YYY	SYS	aircraft/props/ga/c185e.obj\n"
    );
  std::istringstream designatorsStream (designators);
  std::istringstream planesStream (planes);

  XPInnPlanesTaxonomy tax;
  
  tax.reloadDesignators(designatorsStream, "string");
  tax.reloadPlanes(planesStream, "string");

  std::cout << "Designators:" << std::endl;
  tax.visitDesignators(&designatorVisitor);

  std::cout << std::endl << "Press enter..." << std::endl << std::endl;
  std::cin.get();

  std::cout << "Planes:" << std::endl;
  tax.visitPlanes(&planeVisitor);
  
  std::cout << std::endl << "Press enter..." << std::endl << std::endl;
  std::cin.get();

  std::vector<std::pair<std::string, std::string> > pairs;
  pairs.push_back(std::pair<std::string, std::string>("A319", "BAW"));
  pairs.push_back(std::pair<std::string, std::string>("B744", "KLM"));
  pairs.push_back(std::pair<std::string, std::string>("C170", "RRR"));
  pairs.push_back(std::pair<std::string, std::string>("BLAH", "FOO"));

  for (std::vector<std::pair<std::string, std::string> >::const_iterator it = pairs.begin(); it != pairs.end(); ++it)
  {
    std::cout << "getClosestMatch(\"" << it->first << "\", \"" << it->second << "\") :-" << std::endl;
    std::cout << tax.getClosestMatch(it->first, it->second) << std::endl << std::endl;
  }

  std::cout << std::endl << "Press enter..." << std::endl << std::endl;
  std::cin.get();

  return 0;
}